//
//  E7GameScene.cpp
//  E7_ENGINE
//
//  Created by cc on 12-2-29.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7GameScene.h"

#include "E7GameManager.h"

#include "E7Logic.h"

CE7GameScene::CE7GameScene()
{
}

CE7GameScene::~CE7GameScene()
{
    Stop() ;
}

// 循环.
void CE7GameScene::step(float dt)
{
    m_fTotalTime += dt ;

    if (!s_pGameLayer->Loop())
    {
        if (s_pLogic->s_bIsRestart)
        {
            m_eStep = S_STOP ;
            
            unscheduleAllSelectors() ;
            removeAllChildrenWithCleanup(false) ;
            
            SAFE_RELEASE(s_pGameLayer) ;
            
            m_fTotalTime = INVALID_FLOAT ;
            m_bIsVisible = false ;

            Start() ;
        }
        else
        {
            Stop() ;
        }

        return ;
    }
}

void CE7GameScene::Start()
{
//    printf("enter game scene start function.\n") ;
    if (S_START == m_eStep)
    {
        return ;
    }

    m_eStep = S_START ;
    m_fTotalTime = ZERO_FLOAT ;
    m_bIsVisible = true ;

    if (NULL == s_pGameLayer)
    {
        s_pGameLayer = new CE7GameLayer() ;
    }
    s_pGameLayer->SetGameMode() ;
    addChild(s_pGameLayer) ;

    schedule(schedule_selector(CE7GameScene::step)) ;
}

void CE7GameScene::Stop()
{
    if (S_START != m_eStep)
    {
        return ;
    }

    m_eStep = S_STOP ;
    
    unscheduleAllSelectors() ;
    removeAllChildrenWithCleanup(true) ;

    SAFE_RELEASE(s_pGameLayer) ;

    m_fTotalTime = INVALID_FLOAT ;
    m_bIsVisible = false ;

    g_pGameManager->ChangeSceneState(SS_MENU, false) ;
    //printf("change to stage scene\n") ;
}








